/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import org.jbox2d.common.Vec2D;
import org.jbox2d.dynamics.joints.DistanceJoint;
import org.jbox2d.dynamics.joints.DistanceJointDef;


public class FixedPosition extends GameObject{
    private DistanceJoint joint;
    private float damping;
    private float frequency;
    private Vec2D position;
    public boolean started = false;
    private Part part;
    public FixedPosition() throws WorldsException
    {
        super("FixedPosition");
        addPropChangeHandler("Parent", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                onParentChanged((GameObject)oldValue, (GameObject) value);
                if(oldValue instanceof Part && value != null)
                    stop();
                if(value instanceof Part)
                {
                    start((Part) value);
                }
            }
        });
        addPropChangeHandler("Damping", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                damping = (Float) value;
                if(joint != null)
                    joint.m_dampingRatio = damping;
            }
        });
        addPropChangeHandler("Frequency", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                frequency = (Float) value;
                if(joint != null)
                    joint.m_frequencyHz = frequency;
            }
        });
        addPropChangeHandler("Position", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                position = (Vec2D) value;
                if(joint != null)
                {
                    joint.m_localAnchor2 = position;
                    part.wakeUpConnected();
                    part.getBody().wakeUp();
                }
            }
        });
        //x_;
    }
    public void start(Part p) throws WorldsException
    {
        if(started)
            return;
        part = p;
        started = true;
        setProp("Position", p.getPosition(), true);
        DistanceJointDef jd = new DistanceJointDef();
        jd.body1 = p.getBody();
        jd.body2 = world.getGroundBody();
        jd.dampingRatio = damping;
        jd.frequencyHz = frequency;
        jd.localAnchor1 = p.getBody().getLocalCenter();
        jd.localAnchor2 = position;
        jd.length = 0;
        joint = (DistanceJoint) world.addJoint(jd);
        if(p instanceof Box || p instanceof Triangle)
        {
            p.getBody().m_angularDamping = 0.5f;
        }
    }
    private void stop()
    {
        started = false;
        world.destroyJoint(joint);
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
        stop();
    }
    @Override
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
        if(getParent() instanceof Part)
        {
            start((Part)getParent());
        }
    }
}
